package com.baili.dao.admin;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.baili.controller.admin.AdminController;
import com.baili.dao.base.BaseDao;
import com.baili.dto.Defect;
import com.baili.dto.SelectUserInput;
import com.baili.util.StringUtil;
import com.baili.util.UploadFile;

/** 
 * @author iceX
 * @version 创建时间：2016年3月25日 上午10:07:01 
 * @Name: CallDaoImpl.java
 */ 
@Repository("AdminDao")
public class AdminDaoImpl extends BaseDao implements AdminDao{
	
	private static Logger log = Logger.getLogger(AdminDaoImpl.class);

	@Override
	public List adminApCount() {
		
		return this.sqlSessionTemplate.selectList("admin.adminApCount");
	}

	@Override
	public List adminApList(int approve) {
		
		return this.sqlSessionTemplate.selectList("admin.adminApList" ,approve);
	}

	@Override
	public List adminApById(int id) {
		
		return this.sqlSessionTemplate.selectList("admin.adminApById" ,id);
	}

	@Override
	public void updateApState(SelectUserInput user) {
		
		 this.sqlSessionTemplate.update("admin.updateApState" ,user);
	}

	@Override
	public List getAdminName(SelectUserInput user) {
		
		return this.sqlSessionTemplate.selectList("admin.getAdminName" ,user);
	}
	
	@Override
	public List getProcess() {
		
		return this.sqlSessionTemplate.selectList("admin.getProcess");
	}
	@Override
	public List getProcessById(int id) {
		
		Map map = new HashMap();
		
		map.put("id", id);
		
		return this.sqlSessionTemplate.selectList("admin.getProcessById",map);
	}

	@Override
	public List adminApByRoleName(SelectUserInput user) {
		List lst = new ArrayList();
		
		if(user.getName().substring(0,2).equals("操作") || user.getName().substring(0,2).equals("配帮")){
			
			lst = this.sqlSessionTemplate.selectList("admin.adminApByRoleName" ,user);
		
		}else if(user.getName().substring(0,2).equals("成品") || user.getName().substring(0,2).equals("检验") || user.getName().substring(0,3).equals("半成品")){
		
			lst = this.sqlSessionTemplate.selectList("admin.adminApByProcessName" ,user);
		
		}
		return lst;
	}
	
	@Override
	public void uploadfile(CommonsMultipartFile file) {
		try {
			
			String image_path = "/images/";

			String video_path = "/video/";
			
			String url = "182.50.1.8:18016";
			
			// 取文件格式后缀名
			String type = file.getOriginalFilename().
					substring(file.getOriginalFilename().indexOf("."));
			
			String sysDate = new StringUtil().refFormatNowDate();
			
			String str = new StringUtil().getRandomString(10, "ilu");
			
			String filename =  "BL" + str + "_" + sysDate + type;// 取当前时间戳作为文件
			
			//引用文件上传工具类
			UploadFile uploadFile = new UploadFile();

			if(type.equals(".jpg") || type.equals(".JPG") || type.equals(".png") || type.equals(".PNG") 
					|| type.equals(".gif") || type.equals(".JPEG") || type.equals(".jpeg")){
				
				//request.getSession().getServletContext().getRealPath("/image/" + filename);//存放到tomcat目录的下面
				image_path = image_path + filename;// 存放位置
				
				//uploadFile.uploadFile(file , url+image_path);
				
				//将url和文件名存到数据库存入数据库
				Map map = new HashMap();
				map.put("filename", filename);
				map.put("path", url+image_path);
				this.sqlSessionTemplate.insert("admin.uploadPicture" ,map);
			}else if(type.equals(".AVI") || type.equals(".avi") || type.equals(".wma") || type.equals(".WMA")
					|| type.equals(".rmvb") || type.equals(".RMVB") || type.equals(".flash") || type.equals(".FLASH")
					|| type.equals(".mp4") || type.equals(".MP4") || type.equals(".mkv") || type.equals(".MKV")
					|| type.equals(".wmv") || type.equals(".WMV")){
				
				video_path = video_path + filename;// 存放位置
			
				//uploadFile.uploadFile(file , url+video_path);
				
				//将url和文件名存到数据库存入数据库
				Map map = new HashMap();
				map.put("filename", filename);
				map.put("path", url+video_path);
				this.sqlSessionTemplate.insert("admin.uploadVideo" ,map);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void uploadfiles(InputStream file,String fileName,String type) throws IOException {
			
			//引用文件上传工具类
			UploadFile uploadFile = new UploadFile();
			
			String directory = File.separator+"opt";
			
			String image_path = File.separator+"images"+File.separator;

			String video_path = File.separator+"video"+File.separator;
			
			//String image_path = "E:"+File.separator+"images"+File.separator;
			
			//String video_path = "E:"+File.separator+"videos"+File.separator;
			
			String url = "http"+":"+"/"+"/"+"182.50.1.8:18016";
			//String url = "http"+":"+"/"+"/"+"101.201.154.205:8081";
				
		// 取文件格式后缀名
			//String type = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
				
			String filename =  fileName+ type;// 取当前时间戳作为文件
			if(type.equals(".jpg") || type.equals(".JPG") || type.equals(".png") || type.equals(".PNG") 
						|| type.equals(".gif") || type.equals(".JPEG") || type.equals(".jpeg")){
					
		// uploadFile.SFTPuploadFile(files[i],directory+image_path,filename);
			//	uploadFile.uploadFile(file,directory+image_path+filename);
				uploadFile.uploadFile(file,directory+image_path+filename);
					
				//将url和文件名存到数据库存入数据库
				Map map = new HashMap();
					
				map.put("filename", filename);
					
				map.put("path", url+image_path+filename);
					
					//将url存入数据库
				this.sqlSessionTemplate.insert("admin.uploadPicture" ,map);
					
			}else if(type.equals(".AVI") || type.equals(".avi") || type.equals(".wma") || type.equals(".WMA")
						|| type.equals(".rmvb") || type.equals(".RMVB") || type.equals(".flash") || type.equals(".FLASH")
						|| type.equals(".mp4") || type.equals(".MP4") || type.equals(".mkv") || type.equals(".MKV")){
					
		// uploadFile.SFTPuploadFile(files[i],directory+video_path,filename);
				//uploadFile.uploadFile(file,directory+video_path+filename);
				uploadFile.uploadFile(file,directory+video_path+filename);
				
				//将url和文件名存到数据库存入数据库
				Map map = new HashMap();
					
				map.put("filename", filename);
					
				map.put("path", url+video_path+filename);
					
				//将url存入数据库
				this.sqlSessionTemplate.insert("admin.uploadVideo" ,map);
					
			}
	}
	
	@Override
	public void uploadQualityCheck(Defect defect) {
		
		this.sqlSessionTemplate.insert("admin.uploadQualityCheck" ,defect);
		
	}

	@Override
	public List getVersion() {
		
		return this.sqlSessionTemplate.selectList("admin.getVersion");
	}
	@Override
	public void callSet(String callType,Integer processId,Integer charge) {
		
		Map map = new HashMap();
		
		map.put("callType", callType);
		
		map.put("processId", processId);
		
		map.put("charge", charge);
		
		List lst = this.sqlSessionTemplate.selectList("admin.getCall",map);
		
		if(lst == null || lst.size()==0){
			
			this.sqlSessionTemplate.insert("admin.callSet",map);
		}
	}
	@Override
	public void defectSet(String defectName,Integer processId) {
		
		Map map = new HashMap();
		
		map.put("defectName", defectName);
		
		map.put("processId", processId);
		
		List lst = this.sqlSessionTemplate.selectList("admin.getDefect",map);
		
		if(lst == null || lst.size()==0){
			
			this.sqlSessionTemplate.insert("admin.defectSet",map);
		}
	}
	
	@Override
	public void insertSize(Integer size,Integer sex) {
		
		Map map = new HashMap();
		
		map.put("size", size);
		map.put("sex", sex);
		
		this.sqlSessionTemplate.insert("admin.insertSize",map);
	}
	
	@Override
	public void deleteSize(Integer sizeId) {
		
		Map map = new HashMap();
		
		map.put("sizeId", sizeId);
			
		this.sqlSessionTemplate.delete("admin.deleteSize",map);
	}
	@Override
	public void deleteDefectType(Integer defectId) {
		
		Map map = new HashMap();
		
		map.put("defectId", defectId);
		
		this.sqlSessionTemplate.delete("admin.deleteDefectType",map);
	}
}
